

* Open log
capture log close
log using "Data analysis\cbld-management01", replace text


* ****************************************************************************
* Processing of results of computational experiments for further data analysis
* ****************************************************************************

* Programme:	cbld-management01.do
* Project:		Council coalition building
* Author:		Frank Haege, Department of Politics and Administration, University of Limerick
* Contact:		frank.haege@ul.ie

* Description
*************
* This do-file imports the data generated by the computational experiments,
* constructs some additional variablesand, and generates cross-sectional data sets
* by collapsing time-steps within simulation runs


* Set up Stata
version 11
clear all
macro drop _all
set linesize 80
set more off


* Process the results of each computational experiment
******************************************************

* Loop through each set of experimental results
foreach i in 1 2 3 {

	* Load data set generated by computational experiment
	insheet using "Experiments\Experiment0`i'\cbld-model-121020-exp0`i'.csv", clear
	sort runnumber step
	
	* Drop redundant variables
	drop numberofmemberstates sequenceofmoves regimeparameters
	
	* Order variables
	order runnumber step randomseed numsequenceofmoves numregimeparameters countturtles
	
	* Rename variables
	rename runnumber runno
	rename randomseed rseed
	rename numsequenceofmoves sequence
	rename numregimeparameters regime
	rename countturtles nostates
	rename votingthreshold vthresh
	rename firstcriteriontotal firsttotal
	rename firstwinningthreshold firstwin
	rename firstblockingthreshold firstblock
	rename secondcriteriontotal secondtotal
	rename secondwinningthreshold secondwin
	rename secondblockingthreshold secondblock
	rename thirdcriteriontotal thirdtotal
	rename thirdwinningthreshold thirdwin
	rename thirdblockingthreshold thirdblock
	rename countblockingmemberstates noblock
	rename countpatcheswithcountturtleshere nocoal
	rename maxmyfirstcriterionsizeofturtles firstmax
	rename minmyfirstcriterionsizeofturtles firstmin
	rename maxmysecondcriterionsizeofturtle secondmax
	rename minmysecondcriterionsizeofturtle secondmin
	rename maxmythirdcriterionsizeofturtle thirdmax
	rename minmythirdcriterionsizeofturtle thirdmin
	rename precisionmeanlistofdistances1 meandist
	rename precisionmedianlistofdistances1 meddist
	rename precisionminlistofdistances1 mindist
	rename precisionmaxlistofdistances1 maxdist
	
	* Collapse results along time dimension
	sort runno step
	collapse (first) rseed=rseed sequence=sequence regime=regime nostates=nostates vthresh=vthresh /*
		*/ firsttotal=firsttotal firstwin=firstwin firstblock=firstblock /*
		*/ secondtotal=secondtotal secondwin=secondwin secondblock=secondblock /*
		*/ thirdtotal=thirdtotal thirdwin=thirdwin thirdblock=thirdblock /*
		*/ (last) noblock=noblock nocoal=nocoal step=step/*
		*/ firstmax=firstmax firstmin=firstmin secondmax=secondmax secondmin=secondmin thirdmax=thirdmax thirdmin=thirdmin /*
		*/ (first) meandist=meandist meddist=meddist /*
		*/ mindist=mindist maxdist=maxdist, by(runno)
	
	* Label variables
	label var runno "Run number"
	label var rseed "Random seed"
	label var sequence "Order of agent execution"
	label var regime "Regime parameters"
	label var nostates "Number of member states"
	label var firsttotal "Total number of votes (first criterion)"
	label var vthresh "Voting threshold"
	label var firstwin "Winning coalition size (first criterion)"
	label var firstblock "Blocking coalition size (first criterion)"
	label var secondtotal "Total number of votes (second criterion)"
	label var secondwin "Winning coalition size (second criterion)"
	label var secondblock "Blocking coalition size (second criterion)"	
	label var thirdtotal "Total number of votes (third criterion)"
	label var thirdwin "Winning coalition size (third criterion)"
	label var thirdblock "Blocking coalition size (third criterion)"	
	label var step "Time periods"
	label var noblock "Number of states in blocking coalitions"
	label var nocoal "Number of coalitions"
	label var firstmax "Size of largest coalition (first criterion)"
	label var firstmin "Size of smallest coalition (second criterion)"
	label var secondmax "Size of largest coalition (second criterion)"
	label var secondmin "Size of smallest coalition (second criterion)"
	label var thirdmax "Size of largest coalition (third criterion)"
	label var thirdmin "Size of smallest coalition (third criterion)"
	label var meandist "Mean distance between positions"
	label var meddist "Median distance between positions"
	label var mindist "Minimum distance between positions"
	label var maxdist "Maximum distance between positions"
	
	* Save as Stata data set
	sort runno
	compress
	save "Experiments\Experiment0`i'\cbld-model-121020-exp0`i'.dta", replace
}


* Exit do-file
log close
exit
